package com.taormina.searchservice;

import java.io.IOException;

import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.taormina.search.schema.SearchRequest;
import com.taormina.search.schema.SearchResponse;


@Endpoint
public class SearchServiceEndpoint {

	private static final String NAMESPACE_URI = "http://taormina.com/search/schemas";
	private SearchService searchService;
		
	@Autowired
	public SearchServiceEndpoint(SearchService searchService) throws JDOMException {
		this.searchService = searchService;
	}
	
	@PayloadRoot(namespace=NAMESPACE_URI, localPart="SearchRequest")
	@ResponsePayload
	public SearchResponse handleWebSearchQuery(@RequestPayload SearchRequest searchRequest) {
		try {
			return searchService.getSearchResults(
					searchRequest.getQueryString1(), 
					searchRequest.getQueryString2(), 
					searchRequest.getQueryString3());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
